#!/bin/bash

function get_inode()
{
	if [[ ! -e $1 ]]; then
		echo -e "[ERROR] The file not found: $1" >&2
		return 1
	fi

	ls -i $1 | awk '{print $1}'
}

function to_lower()
{
    echo "$1" | tr '[:upper:]' '[:lower:]'
}

function to_upper()
{
    echo "$1" | tr '[:lower:]' '[:upper:]'
}

function abs_path()
{
	realpath $1
}

function abs_path_level()
{
	if [[ ! -d $1 ]]; then
		echo -e "[ERROR] The directory not found: $1" >&2
		return 1
	fi

	cd $(abs_path $1)
	pwd | awk -F '/' '{print NF}'
	cd - &> /dev/null
}

function get_kernel_vermagic()
{
	if [[ $# -le 0 ]]; then
		return
	fi

	local kernel_image_file=$1
	strings ${kernel_image_file} | grep -i "Linux version [0-9]" | awk '{print $3}'
}

function get_kernel_vermagic_rksuffix()
{
	if [[ $# -le 0 ]]; then
		return
	fi

	local kernel_vermagic=$1
	echo ${kernel_vermagic} | cut -d- -f3-
}

function get_cur_kernel_vermagic()
{
	get_kernel_vermagic "${L4T_TOP}/kernel/Image"
}

function get_module_vermagic()
{
	if [[ $# -le 0 ]]; then
		return
	fi

	local module_file=$1
	modinfo -F vermagic ${module_file} 2>/dev/null | awk '{print $1}'
}

function check_device_recovery()
{
	local board_usb=$(lsusb -d 0x0955:)
	if [[ -n ${board_usb} ]]; then
		echo "Y"
	else
		echo "N"
	fi
}
